﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using imaginecup.information;
using imaginecup.objectManager;
using Imaginecup.gameState.malaria.componentInit;
using Microsoft.Xna.Framework;
using Imaginecup.sprite.rotate;
using Imaginecup.gameState.malaria.component.util;
using imaginecup.util;
using Imaginecup.util.compute;
using Imaginecup.util.collision;
using Imaginecup.util;

namespace Imaginecup.gameState.malaria.component.entity.fieldItem
{
    public enum FrogState { CONTACT , SHOOT , DRAG , EAT}
    public class CmpFieldFrog : BaseEntityComponent,  IRotateInfo
    {
        private ICmpMalaria m_CatchMalaria;
        private double m_DelayTime;
        private FrogState m_State;
        private float m_DragVelocity = 150;
        private float m_ViewRange = 100;
        private float m_CurrentDistance;


        public CmpFieldFrog(GObjectID a_GID, ObjectManager objMan)
            : base(a_GID, objMan, ComponentTypeID.CID_FIELD_FROG_ENTITY)
        {
            m_CatchMalaria = null;

            m_State = FrogState.CONTACT;
        }


        public float GetLength()
        {
            if (m_State == FrogState.CONTACT || m_State == FrogState.EAT) return 0;

            EntityInfo catchMalariaEntity = (EntityInfo)m_CatchMalaria.GetEntityInfo();
            m_CurrentDistance = Vector2.Distance(m_Info.GetEntityCenter(), catchMalariaEntity.GetEntityCenter());

            return m_CurrentDistance;
        }

        public float GetAngle()
        {
            if( m_State == FrogState.CONTACT || m_State == FrogState.EAT ) return 0;

            EntityInfo catchMalariaEntity = (EntityInfo)m_CatchMalaria.GetEntityInfo();

            Vector2 Src = m_Info.GetEntityCenter();
            Vector2 Dest = catchMalariaEntity.GetEntityCenter();

            float angle = (float)Math.Atan2((double)(Dest.Y - Src.Y), (double)(Dest.X - Src.X));
            return angle;
        }

        
        public override void Update(GameTime gameTime)
        {
            m_DelayTime -= gameTime.ElapsedGameTime.TotalSeconds;

            if (m_DelayTime <= 0 && m_State == FrogState.CONTACT)
            {
                m_ObjectMan.DestroyObject(m_ObjectID);
            }

            else // 상태별 처리.
            {
                if (m_State == FrogState.CONTACT && m_CatchMalaria != null)
                {
                    if (m_CatchMalaria.IsMoveAble())
                    {
                        m_State = FrogState.SHOOT;
                        m_CatchMalaria.StopMove();
                    }
                    else
                    {
                        m_CatchMalaria = null;
                    }
                }
                else if (m_State == FrogState.SHOOT)
                {
                    SoundMan.GetSoundMan().playEffect("Frog");
                    m_State = FrogState.DRAG;
                }
                else if (m_State == FrogState.DRAG)
                {
                    DragProcess(gameTime);
                }

                else if (m_State == FrogState.EAT)
                {
                    m_CatchMalaria.AddDemage(1); // 자동으로 죽으면서 프로그 스테이트 변경.
                }
            }

        }

        public override EMessageResult HandleMessage(GMessage a_Msg)
        {
            if (a_Msg.m_Type.MessageTypeID == MessageTypeID.MT_CHANGE_MALARIAPOSITION.MessageTypeID)
            {
                if (m_State  == FrogState.CONTACT)
                {
                    ICmpMalaria malaria = (ICmpMalaria)a_Msg.m_Data;
                    ContactProcess(malaria);
                }
            }

            else if (a_Msg.m_Type.MessageTypeID == MessageTypeID.MT_MALARIA_DIE.MessageTypeID)
            {
                //죽었다. 다른 목표를 찾으면 된다.
                if (m_CatchMalaria != null && m_CatchMalaria.GetObjectID() == a_Msg.m_FromID)
                {
                    m_CurrentDistance = 0;
                    m_CatchMalaria = null;
                    m_State = FrogState.CONTACT;
                }
            }

            return base.HandleMessage(a_Msg);
        }
            

        private void ContactProcess( ICmpMalaria cmpMalaria )
        {
            EntityInfo newMalariaEntity = (EntityInfo)cmpMalaria.GetEntityInfo();

            float newDistance = Vector2.Distance(m_Info.GetEntityCenter(), newMalariaEntity.GetEntityCenter());

            if (newDistance > m_ViewRange )
                return ;

            if (m_CatchMalaria == null)
            {
                m_CurrentDistance = newDistance;
                m_CatchMalaria = cmpMalaria;
            }
            // 현재 목표물보다 가까우면 목표물 변경
            else if (m_CurrentDistance > newDistance)
            {
                m_CatchMalaria = cmpMalaria;
                m_CurrentDistance = newDistance;
            }
        }

        private void DragProcess(GameTime gameTime)
        {
            EntityInfo malariaInfo = (EntityInfo)m_CatchMalaria.GetEntityInfo();

            if (Collision.CollisionByRect(m_Info, malariaInfo))
            {
                m_State = FrogState.EAT;
            }
            else
            {
                Vector2 malariaCenter = malariaInfo.GetEntityCenter();
                Vector2 direction = Vector2.Subtract(m_Info.GetEntityCenter(), malariaCenter);//말라리아쪽에서 개구리쪽으로 이동.
                direction.Normalize();

                malariaCenter.X += direction.X * m_DragVelocity * (float)gameTime.ElapsedGameTime.TotalSeconds;
                malariaCenter.Y += direction.Y * m_DragVelocity * (float)gameTime.ElapsedGameTime.TotalSeconds;

                malariaInfo.SetEntityCenter(malariaCenter);
                m_CatchMalaria.SetEntityInfo(malariaInfo);
            }                        

        }


        public void SetDelayTime(double delay)
        {
            m_DelayTime = delay;
        }
    }
}
